package org.xlin.biz.dao.table;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.xlin.biz.dao.AbstBaseDao;
import org.xlin.biz.util.UtilHandler;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

/**
 * 
 * 桌位数据处理类
 * @author xl
 *
 */
@Repository
public class TableDao extends AbstBaseDao<Map<String,?>>{
	
	/**
	 * 增加桌位
	 */
	private final static String AddRecord = " insert into t_mg_table(num,type,descr) "
			+ " values(:num,:type,:descr) ";
	
	/**
	 * 修改桌位
	 */
	private final static String ModfiyRecord = " update t_mg_table t "
			+ " set t.num=:num, "
			+ " t.type=:type, "
			+ " t.descr=:descr "
			+ " where t.id=:id ";
	
	/**
	 * 删除桌位(逻辑删除)
	 */
	private final static String DeleteRecord = " update t_mg_table t set t.valid='0' where t.id=:id ";
	
	/**
	 * 分页查询桌位列表(含是否已使用＝>仅可能存在1次未结账就餐单)
	 * @param pageable
	 * @param condition
	 * @return
	 */
	public Page<Map<String, ?>> queryRecord4Page(Pageable pageable, Map<String, Object> condition){
		//查询列表Sql
        StringBuilder querySql = new StringBuilder();
        querySql.append(" select t.*,r.id as repastid from t_mg_table t ")
        	.append(" left join t_mg_repast r on t.id=r.tbid and r.state='0' ")
        	.append(" where t.valid='1' {whereSql} ");
        //查询记录数Sql
        StringBuilder countSql = new StringBuilder();
        countSql.append(" select count(t.id) from t_mg_table t ")
        	.append(" left join t_mg_repast r on t.id=r.tbid and r.state='0' ")
	    	.append(" where t.valid='1' {whereSql} ");
        //查询条件Sql
        StringBuilder whereSql = this.getWhereSqlByParseCondition(condition);
        return this.queryPage4Mysql(querySql, countSql, whereSql, pageable, condition, new RowMapper<Map<String, ?>>() {
			@Override
            public Map<String, ?> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String,Object> info=Maps.newHashMap();
				info.put("ID", rs.getObject("ID"));
				info.put("NUM", rs.getObject("NUM"));
				info.put("TYPE", rs.getObject("TYPE"));
				info.put("DESCR", rs.getObject("DESCR"));
				info.put("CREATED", rs.getObject("CREATED"));
				info.put("REPASTID",rs.getObject("REPASTID"));
				return info;
            }
        });
	}
	
	/**
     * 条件解析
     * @param condition
     * @return
     */
    public StringBuilder getWhereSqlByParseCondition(Map<String, Object> condition) {
        StringBuilder whereSql = new StringBuilder();
        //桌位编码
        if (condition.containsKey("searchName")
        		&& !"".equals(condition.get("searchName"))) {
        	whereSql.append(" and t.num like :searchName escape '/' ");
        	condition.put("searchName", "%" + UtilHandler.handleSqlParam((String) condition.get("searchName")) + "%");
        }
        //桌位类型
        if (condition.containsKey("searchType")
        		&& !"".equals(condition.get("searchType"))) {
        	whereSql.append(" and t.type = :searchType ");
        }
        //使用状态
        if (condition.containsKey("searchState")
        		&& !"".equals(condition.get("searchState"))) {
        	if("0".equals(condition.get("searchState"))){
        		whereSql.append(" and r.id is null ");
        	}else if("1".equals(condition.get("searchState"))){
        		whereSql.append(" and r.id is not null ");
        	}
        }
        return whereSql;
    }
    
    /**
	 * 增加桌位
	 * @param condition
	 * @return
	 */
	public void addRecord(Map<String,Object> condition){
		this.namesUpdateForMap(AddRecord, condition);
	}
	
	/**
	 * 更新桌位
	 * @param condition
	 * @return
	 */
	public int modfiyRecord(Map<String,Object> condition){
		return this.namesUpdateForMap(ModfiyRecord, condition);
	}
	
	/**
	 * 删除桌位
	 * @param condition
	 * @return
	 */
	public void deleteRecord(Map<String,Object> condition){
		this.namesUpdateForMap(DeleteRecord, condition);
	}

}
